package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.FeedbackRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.FeedbackSearchRequest;
import com.tuanzi.loan.vo.response.family.FeedbackSearchResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.Tables.FEEDBACK_INFO;

@Repository
public class FeedbackRepositoryImpl extends JOOQBaseRepositoryPlus implements FeedbackRepository {

	@Override
	protected TableImpl getTable() {
		return FEEDBACK_INFO;
	}

	@Override
	public Page<FeedbackSearchResponse> list(FeedbackSearchRequest searchRequest, Pageable pageable) {
		fields();

		between(FEEDBACK_INFO.CREATE_TIME, searchRequest.getCreateStartTime(),searchRequest.getCreateEndTime());
		in(FEEDBACK_INFO.PRODUCT_ID, searchRequest.getProductIdList());
		eq(FEEDBACK_INFO.FEEDBACK_TYPE, searchRequest.getFeedbackType());
		eq(FEEDBACK_INFO.IS_RESOLVED, searchRequest.getIsResolved());
		orderBy(FEEDBACK_INFO.CREATE_TIME, Sort.Direction.DESC);

		return search(pageable, record -> {
			FeedbackSearchResponse response = new FeedbackSearchResponse();
			response.setId(record.get(FEEDBACK_INFO.ID));
			response.setCreateTime(record.get(FEEDBACK_INFO.CREATE_TIME));
			response.setProductId(record.get(FEEDBACK_INFO.PRODUCT_ID));
			response.setLoginPhoneNo(record.get(FEEDBACK_INFO.LOGIN_PHONE_NO));
			response.setReservedPhoneNo(record.get(FEEDBACK_INFO.RESERVED_PHONE_NO));
			response.setDescription(record.get(FEEDBACK_INFO.DESCRIPTION));
			response.setFeedbackType(record.get(FEEDBACK_INFO.FEEDBACK_TYPE, Integer.class));
			response.setIsResolved(record.get(FEEDBACK_INFO.IS_RESOLVED, Boolean.class));
			response.setEntranceId(record.get(FEEDBACK_INFO.ENTRANCE_ID));
			return response;
		});
	}

	@Override
	public void updateIsResolved(Integer id, Byte isResolved) {
		dslContext.update(FEEDBACK_INFO).set(FEEDBACK_INFO.IS_RESOLVED, isResolved).where(FEEDBACK_INFO.ID.eq(id)).execute();
	}

	@Override
	public void delete(Integer id) {
		dslContext.deleteFrom(FEEDBACK_INFO).where(FEEDBACK_INFO.ID.eq(id)).execute();
	}
}
